from utils.goods import get_categories

import os
import time
from django.conf import settings
from django.template import loader
from apps.contents.models import ContentCategory


def generate_static_index_html():
    '''生成静态首页'''

    # 形成日志
    print('%s: generate_static_index_html' % time.time())

    # 或取商品频道分类
    categories = get_categories()

    # 从数据库获取广告内容
    contents = {}
    content_categories = ContentCategory.objects.all()
    for cart in content_categories:
        contents[cart.key] = cart.content_set.filter(status=True).order_by('sequence')
    # 渲染模板
    context = {
            'categories': categories,
            'contents': contents
    }



    template = loader.get_template('index.html')

    # 渲染
    html_text = template.render(context)

    # 将首页html字符串写入到指定目录，命名'index.html'
    file_path = os.path.join(os.path.dirname(settings.BASE_DIR), 'front_end_pc/index.html')
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(html_text)